package com.cmcc.driver.ui.profile.viewModel

import androidx.lifecycle.*
import com.cmcc.driver.data.DriverRequest
import com.cmcc.driver.data.entity.request.CarUpdateRequest
import com.cmcc.driver.data.entity.response.CarCategory
import com.cmcc.driver.data.entity.response.CarInfo
import com.cmcc.driver.data.entity.response.ResMsg
import com.cmcc.shared.Event
import com.cmcc.shared.config.SharedConfig
import com.cmcc.shared.utils.L
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.atomic.AtomicInteger

/**
 * Author: jiangxin
 * Date: 2019/11/12
 * Description:
 *
 * 车辆管理页控制器
 * 1.绑定点击事件
 * 2.请求我的车辆信息
 * 3.修改车辆信息
 *
 */
class TaxiInfoViewModel : ViewModel() {

    val _confirmSelector = MutableLiveData<Event<Unit>>()
    //    val updateCarInfo = MutableLiveData<Int>()
    val resMsg = MutableLiveData<ResMsg>()

    val carInfo = MutableLiveData<CarInfo>()

    val carInfoCode = MutableLiveData<Int>()

    val carCategory = MutableLiveData<List<CarCategory>>()

    var flag = AtomicInteger(2)

    val radioMsg = MediatorLiveData<Boolean>()

    fun onConfirmClick() {
        _confirmSelector.value = Event(Unit)
    }


    fun updateCarInfo(carInfo: CarUpdateRequest) {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().updateCarInfo(carInfo)
                if (response.isSuccessful) {
                    val data = response.body()
//                    updateCarInfo.value = data?.code
                    if (data?.code == 200) {
                        SharedConfig.cardId = carInfo.idCard
                    }
                    resMsg.value = ResMsg(data?.code ?: 0, data?.msg ?: "")
                } else {

                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    /**
     * 获取车辆信息及driverId
     */
    fun requestCarInfo(): MutableLiveData<CarInfo> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().getCarInfo()
                if (response.isSuccessful) {
                    val body = response.body()
                    carInfoCode.value = body?.code
                    if (body?.data?.isNotEmpty() == true) {
                        carInfo.value = body.data[0]
                    }
                } else {
                    L.d("response failed")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
        return carInfo
    }

    fun requestCarCategory() {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = DriverRequest.getInstance().getCarCategory()
                if (response.isSuccessful) {
                    val body = response.body()
                    if (body?.data?.isNotEmpty() == true) {
                        carCategory.value = body.data.filterNot {
                            it.code.equals("vehicle_type_item_any")
                        }
                    }
                } else {
                    L.d("response failed")
                }
            } catch (e: Exception) {
                L.d("e :" + e.message + "," + e.cause)
            }
        }
    }

    fun plus() {
        radioMsg.addSource(carInfoCode) {
            val minus = flag.decrementAndGet()
            if (minus == 0) {
                radioMsg.postValue(minus == 0)
            }
        }
        radioMsg.addSource(carCategory) {
            val minus = flag.decrementAndGet()
            if (minus == 0) {
                radioMsg.postValue(minus == 0)
            }
        }
    }

}